
********************************************************************************	 
//initial sample: students enrolled in a high school program providing access to higher education upon completion
cd $pathdata_education
use npid tilgdato lopenr_kurs nus2000 heldel elevstatus skole_foretak skolekom eierf komp if komp == 1 & !missing(lopenr_kurs) using f_kurs_1974-2018.dta, clear
duplicates drop

gen year = real(substr(string(tilgdato), 1, 4))
gen month = real(substr(string(tilgdato), 5, 2))
keep if month>=8

gen public = inlist(eierf, 1, 2, 4, 5) if !inlist(eierf, 9, .)
drop eierf

encode elevstatus, gen(status)
sum status
replace status = `r(max)' + 1 if status == 1
drop elevstatus

replace skolekom = . if skolekom == 9999
duplicates drop

gsort npid lopenr heldel status -public skole_foretak -skolekom 
bysort npid lopenr: gen n = _n
bysort npid lopenr (n): replace status=status[_n+1] if missing(status)
bysort npid lopenr (n): replace skole_foretak=skole_foretak[_n+1] if missing(skole_foretak)
bysort npid lopenr (n): replace skole_foretak=skole_foretak[_n+1] if missing(skole_foretak)
bysort npid lopenr (n): replace skolekom=skolekom[_n+1] if missing(skolekom)
keep if n == 1
drop n

bysort npid tilgdato: gen N_tilg = _N
bysort npid year: gen N_year = _N
bysort npid (tilgdato): keep if _n == 1
drop tilgdato month
keep if year>=2005 & year<=2009 //for the first time between 2005 & 2009

gen fylke = substr(string(skolekom), 1, 1)
replace fylke = substr(string(skolekom), 1, 2) if length(string(skolekom)) == 4
destring fylke, replace

format nus2000 %20.0g

ds npid, not
foreach var in `r(varlist)' {
	rename `var' `var'_vg3
}

cd $pathdata_processed
save terminales, replace
********************************************************************************
keep npid year_vg3

cd $pathdata_education
merge 1:m npid using f_kurs_1974-2018.dta, keep(1 3) nogen

keep if real(substr(string(tilgdato), 1, 4)) <= year_vg3

bysort npid komp: gen nvals = _n == 1
replace nvals = 0 if missing(komp)
bysort npid: egen nb_komp = sum(nvals)
keep npid nb_komp
duplicates drop

cd $pathdata_processed
merge 1:1 npid using terminales, nogen
save terminales, replace
********************************************************************************
